frappe.query_reports["Financial Statements CN"] = {
	"filters": [
		{
			"fieldname": "company",
			"label": __("Company"),
			"fieldtype": "Link",
			"options": "Company",
			"default": frappe.defaults.get_user_default("Company"),
			"reqd": 1
		},
		{
			"fieldname": "fiscal_year",
			"label": __("Fiscal Year"),
			"fieldtype": "Link",
			"options": "Fiscal Year",
			"default": frappe.defaults.get_user_default("fiscal_year"),
			"reqd": 1,
			"on_change": function(query_report) {
				var fiscal_year = query_report.get_values().fiscal_year;
				if (!fiscal_year) {
					return;
				}
				frappe.model.with_doc("Fiscal Year", fiscal_year, function(r) {
					var fy = frappe.model.get_doc("Fiscal Year", fiscal_year);
					frappe.query_report.set_filter_value({
						"from_date": fy.year_start_date,
						"to_date": fy.year_end_date
					});
				});
			}
		},
		{
			"fieldname": "from_date",
			"label": __("From Date"),
			"fieldtype": "Date",
			"default": frappe.defaults.get_user_default("year_start_date"),
		},
		{
			"fieldname": "to_date",
			"label": __("To Date"),
			"fieldtype": "Date",
			"default": frappe.defaults.get_user_default("year_end_date"),
		},
		{
			"fieldname": "periodicity",
			"label": __("Periodicity"),
			"fieldtype": "Select",
			"options": ["Yearly", "Half-Yearly", "Quarterly", "Monthly"],
			"default": "Yearly",
			"reqd": 1
		},
		{
			"fieldname": "accumulated_values",
			"label": __("Accumulated Values"),
			"fieldtype": "Check",
			"default": 1
		},
		{
			"fieldname": "include_default_book_entries",
			"label": __("Include Default FB Entries"),
			"fieldtype": "Check",
			"default": 1
		},
		{
			"fieldname": "finance_book",
			"label": __("Finance Book"),
			"fieldtype": "Link",
			"options": "Finance Book"
		},
		{
			"fieldname": "presentation_currency",
			"label": __("Currency"),
			"fieldtype": "Select",
			"options": erpnext.get_presentation_currency_list()
		},
		{
			"fieldname": "cost_center",
			"label": __("Cost Center"),
			"fieldtype": "Link",
			"options": "Cost Center"
		},
		{
			"fieldname": "project",
			"label": __("Project"),
			"fieldtype": "Link",
			"options": "Project"
		},
		// 新增比较列选项
		{
			"fieldname": "enable_year_on_year",
			"label": __("Year on Year"),
			"fieldtype": "Check",
			"default": 0,
			"description": __("Show year-over-year comparison")
		},
		{
			"fieldname": "enable_budget",
			"label": __("Against Budgeting"),
			"fieldtype": "Check",
			"default": 0,
			"description": __("Compare actuals against budget"),
			"on_change": function(query_report) {
				var enable_budget = query_report.get_values().enable_budget;
				if (enable_budget) {
					query_report.toggle_filter_display("budget", true);
				} else {
					query_report.toggle_filter_display("budget", false);
				}
			}
		},
		{
			"fieldname": "budget",
			"label": __("Budget"),
			"fieldtype": "Link",
			"options": "Budget",
			"depends_on": "eval:doc.enable_budget == 1",
			"hidden": 1
		},
	],
	
	"formatter": function(value, row, column, data, default_formatter) {
		value = default_formatter(value, row, column, data);
		
		// 预算差异高亮
		if (column.fieldname && column.fieldname.endsWith("_variance_percent")) {
			var variance = parseFloat(data[column.fieldname]);
			if (!isNaN(variance)) {
				if (variance < -5) {
					value = "<span style='color:red'>" + value + "</span>";
				} else if (variance > 5) {
					value = "<span style='color:green'>" + value + "</span>";
				}
			}
		}
		
		// 同比增长高亮
		if (column.fieldname && column.fieldname.endsWith("_yoy")) {
			var growth = parseFloat(data[column.fieldname]);
			if (!isNaN(growth)) {
				if (growth < 0) {
					value = "<span style='color:red'>" + value + "</span>";
				} else if (growth > 0) {
					value = "<span style='color:green'>" + value + "</span>";
				}
			}
		}
		
		return value;
	},
	
	"tree": true,
	"name_field": "account",
	"parent_field": "parent_account",
	"initial_depth": 3
};